package com.sun.showcase.controller.app.mall;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aliyuncs.utils.StringUtils;
import com.sun.showcase.biz.mall.service.CollectionService;
import com.sun.showcase.client.domain.mall.Collection;
import com.sun.showcase.client.query.mall.CollectionQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Result;
import com.sun.showcase.utils.ExecuteResult;

@RestController("appCollectionController")
@RequestMapping(value="/app/collection") 
public class CollectionController{
	
	@Autowired
	private CollectionService collectionService;
	
	private Result result = new Result();

	@RequestMapping(value="collect")
	public Result collect(CollectionQuery collectionQuery) {
		ExecuteResult<String> r = collectionService.collect(collectionQuery);
		result.setSuccess(r.isSuccess());
		if(r.isSuccess()) {
			result.setMsg(r.getSuccessMessage());
		}else {
			result.setMsg(r.getErrorMessages().get(0));
		}
		return result;
	}
	@RequestMapping(value="get")
	public Result get(CollectionQuery collectionQuery) {
		Collection c = collectionService.getByUserAndGoods(collectionQuery);
		result.setSuccess(true);
		result.setData(c!=null);
		return result;
	}
	
	@RequestMapping(value="list")
	public Result list(CollectionQuery collectionQuery) {
		if(StringUtils.isEmpty(collectionQuery.getUserId())) {
			result.setSuccess(false);
			result.setMsg("无效会员信息");
			return result;
		}
		collectionQuery.setOrderColum("create_date");
		DataGrid dg = collectionService.datagrid(collectionQuery);
		result.setSuccess(true);
		result.setData(dg);
		return result;
	}
}
